<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ed="livcos.org/ns/editor"
  xmlns:t="livcos.org/ns/cosmos/type"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:c="livcos.org/ns/cosmos"
  exclude-result-prefixes="ed t c doc">
  
  <xsl:import href="Entity.xsl" />
	
	<xsl:param name="parentId"/>
	<xsl:param name="tmplObjId"/>
	<xsl:param name="tmplId"/>
	<xsl:param name="tmplArg1"/>
	<xsl:param name="tmplArg2"/>
	<xsl:param name="tmplArg3"/>
	<xsl:param name="tmplArg4"/>
	<xsl:param name="tmplArg5"/>
	
  <xsl:variable name="typeBase" select="'/livcos.org/type/editor/'"/>
	
	<xsl:template match="/">
		<xsl:choose>
			<xsl:when test="$tmplObjId">
				<xsl:variable name="data" as="node()?">
					<xsl:variable name="_tmplArgs">
						<xsl:text>arg1=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg1)"/>
						<xsl:text>&amp;arg2=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg2)"/>
						<xsl:text>&amp;arg3=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg3)"/>
						<xsl:text>&amp;arg4=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg4)"/>
						<xsl:text>&amp;arg5=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg5)"/>
					</xsl:variable>
					<xsl:variable name="_tmpl"
						select="doc(concat($tmplObjId, '.view?', $_tmplArgs))/*"/>
					<xsl:choose>
						<xsl:when test="$tmplId">
							<xsl:copy-of select="$_tmpl/*[@id = $tmplId]/t:content/node()"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:copy-of select="$_tmpl/*[1]/t:content/node()"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:apply-templates select="$data" mode="applyNode"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="applyNode">
					<xsl:with-param name="parentNode" select="doc(concat($objId,'.',$scope))"/>
					<xsl:with-param name="id" select="$id"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="node()|@*" mode="applyNode">
		<xsl:variable name="type">
			<xsl:variable name="_type">
				<xsl:variable name="_nsPath">
					<xsl:for-each select="tokenize(replace(namespace-uri(.), '://', '/'), '/')">
						<xsl:if test="position() > 1">/</xsl:if>
						<xsl:if test="matches(., '^[0-9]')">_</xsl:if>
						<xsl:value-of select="."/>
					</xsl:for-each>
				</xsl:variable>
				<xsl:value-of select="$_nsPath"/>
				<xsl:if test="$_nsPath != ''">/</xsl:if>
				<xsl:value-of select="local-name(.)"/>
			</xsl:variable>
			<xsl:choose>
				<xsl:when test="doc-available(concat($typeBase, $_type, '.content'))">
					<xsl:value-of select="$_type"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="." mode="type"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:call-template name="buildEntity">
			<xsl:with-param name="type" select="$type"/>
			<xsl:with-param name="typeInfo" select="document(concat($typeBase, $type, '.content'))/*"/>
		</xsl:call-template>
  </xsl:template>
  
	<xsl:template name="buildEntity">
		<xsl:param name="type"/>
		<xsl:param name="typeInfo"/>
		<entity type="{$type}" objId="{$objId}" scope="{$scope}" id="{$id}">
			<xsl:apply-templates select="$typeInfo/ed:input | $typeInfo/ed:editor">
				<xsl:with-param name="params">
					<xsl:text>type=</xsl:text>
					<xsl:value-of select="$type"/>
					<xsl:text>&amp;objId=</xsl:text>
					<xsl:value-of select="$objId"/>
					<xsl:if test="not(contains($id, '{'))">
						<xsl:text>&amp;id=</xsl:text>
						<xsl:value-of select="$id"/>
					</xsl:if>
					<xsl:text>&amp;scope=</xsl:text>
					<xsl:value-of select="$scope"/>
					<xsl:if test="$tmplObjId">
						<xsl:text>&amp;parentId=</xsl:text>
						<xsl:value-of select="$parentId"/>
						<xsl:text>&amp;tmplObjId=</xsl:text>
						<xsl:value-of select="$tmplObjId"/>
						<xsl:text>&amp;tmplId=</xsl:text>
						<xsl:value-of select="$tmplId"/>
						<xsl:text>&amp;tmplArg1=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg1)"/>
						<xsl:text>&amp;tmplArg2=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg2)"/>
						<xsl:text>&amp;tmplArg3=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg3)"/>
						<xsl:text>&amp;tmplArg4=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg4)"/>
						<xsl:text>&amp;tmplArg5=</xsl:text>
						<xsl:value-of select="encode-for-uri($tmplArg5)"/>
					</xsl:if>
				</xsl:with-param>
			</xsl:apply-templates>
			<menu>
				<xsl:apply-templates select="$typeInfo/ed:menu/*">
					<xsl:with-param name="node" select="."/>
				</xsl:apply-templates>
				<xsl:if test="not($typeInfo/ed:menu/*)">
					<action label="Edit..." type="ShowEditAction"/>
					<action label="Clone" type="CloneAction"/>
					<action label="Cut" type="CutAction"/>
					<action label="Copy" type="CopyAction"/>
					<action label="Paste" type="PasteAction"/>
					<action label="Paste into" type="PasteIntoAction"/>
					<action label="Delete" type="DeleteAction"/>
				</xsl:if>
			</menu>
		</entity>
	</xsl:template>
	
	<xsl:template match="text()" mode="type">
    <xsl:text>_default/text</xsl:text>
  </xsl:template>
  
  <xsl:template match="comment()" mode="type">
    <xsl:text>_default/comment</xsl:text>
  </xsl:template>
  
  <xsl:template match="@*" mode="type">
    <xsl:text>_default/attribute</xsl:text>
  </xsl:template>
  
  <xsl:template match="*" mode="type">
    <xsl:text>_default/element</xsl:text>
  </xsl:template>
  
  <xsl:template match="t:namespace" mode="type">
    <xsl:text>_default/namespace</xsl:text>
  </xsl:template>
  
  <xsl:template match="c:*" mode="type">
    <xsl:value-of select="namespace-uri()"/>
    <xsl:text>/ObjectNode</xsl:text>
  </xsl:template>
  
	<xsl:template match="ed:action">
		<xsl:param name="node"/>
		<action>
			<xsl:apply-templates select="@*">
				<xsl:with-param name="node" select="$node"/>
			</xsl:apply-templates>
			<xsl:variable name="ns_nodes" select="namespace::*[(local-name(.) != 'xml') and (local-name(.) != '')]"/>
			<xsl:for-each-group group-by="." select="$ns_nodes">
				<namespace prefix="{local-name(.)}" uri="{.}"/>
			</xsl:for-each-group>
		</action>
	</xsl:template>
	
	<xsl:template match="ed:action/@*">
		<xsl:copy-of select="."/>
	</xsl:template>
	
	<xsl:template match="ed:action/@enable" priority="2">
		<xsl:param name="node"/>
		<xsl:apply-templates select="$node" mode="enable">
			<xsl:with-param name="action" select=".."/>
		</xsl:apply-templates>
	</xsl:template>
	
	<xsl:template match="ed:input | ed:editor">
		<xsl:param name="params"/>
		<xsl:variable name="uri">
			<xsl:text>EntityEditor.view?</xsl:text>
			<xsl:if test="@id">editorId=<xsl:value-of select="@id"/>&amp;</xsl:if>
			<xsl:value-of select="$params"/>
		</xsl:variable>
		<editor>
			<xsl:copy-of select="@*"/>
			<xsl:copy-of select="doc($uri)/*/node()"/>
		</editor>
	</xsl:template>
	
	<!-- TODO: generated stylesheet for editor and actions (to resolve XPaths) -->
	<xsl:template match="*" mode="enable">
	</xsl:template>
	
	<xsl:template match="doc:image" mode="enable">
		<xsl:param name="action"/>
		<xsl:variable name="e" select="$action/@enable"/>
		<xsl:choose>
			<xsl:when test="$e = 'matches(@ref, ''https?://'')' and matches(@ref, 'https?://')"/>
			<xsl:otherwise>
				<xsl:attribute name="disabled">1</xsl:attribute>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>
